<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 登录公用控制器
 * @author xiahuiqin
 */
class Login extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->model("admin_mdl", 'admin');
    }

    /**
     * 后台入口
     */
    public function index()
    {
        if ($this->session->userdata('name')) {
            redirect('admin/admin/index');
        } else {
            $this->load->view('admin/admin/loginV');
        }
    }

    /**
     * 显示验证码图片
     *
     */
    function show()
    {
        $this->authcode->show();
    }

    /**
     * 登出
     */
    function quit()
    {
        $this->session->sess_destroy();
        redirect('admin/login');
    }

    /**
     * 验证是否是管理员
     */
    function do_post()
    {
        $username = $this->input->post('name', true);
        $password = md5($this->input->post('password', true));

        $where = array('name' => $username, 'password' => $password);

        if ($this->authcode->check($this->input->post('gd_pic')) == false) {
            $data['error'] = '验证码错误！请重新输入。';

            $this->load->view('admin/admin/loginV', $data);
        } else if ($this->authcode->check($this->input->post('gd_pic')) && $this->admin->get_numrows($where) != 1) {
            $data['error'] = '用户名或密码输入不正确!';

            $this->load->view('admin/admin/loginV', $data);
        } else {
            $this->session->set_userdata('name', $username);

            $login_user = $this->admin->get_list($where);

            $id = $login_user[0]['id'];

            $this->session->set_userdata('authority', $login_user[0]['authority']); //管理员权限
            $this->session->set_userdata('password', $login_user[0]['password']); //管理员权限

            $datas['last_login_time'] = date('Y-m-d H:i:s'); //上次登入时间,即将这次的登入时间记作下次用的上次登入时间
            $datas['last_login_ip']   = $this->input->ip_address(); //上次登入ip,放在登录那一块儿做修改

            $this->admin->update_byfield('id', $id, $datas);

            redirect('admin/admin/index');
        }
    }
}

/* End of file login.php */
/* Location: ./application/controllers/adminstrator/login.php */
